// 数组解构
let arr = [1, 2, 3,[4,5]]
let [a, b, c, [d,e=9]] = arr
console.log(a,b,c,d,e)

// 对象解构
let user = {
  name:"aqie",
  age:18
}

let {age:uage,name:uname,sex='男' } = user // 根据键名对应, 起别名
console.log(uname, uage, sex)

// 字符串解构
let str = 'aqie'
let [f,g,h,i] = str
console.log(f,g,h,i)

// json 解构, 必须对应键名
let jsonStr = '{"a1":"hello", "b1":"world"}'
let {a1, b1} = JSON.parse(jsonStr)
console.log(a1, b1)

// 实际应用 对数组参数解构
function foo([a,b,c]){
  console.log(a,b,c)
}
let [aqie=foo(arr)] = []          // 惰性赋值
console.log(aqie)

// 对对象参数解构  对对象返回值解构
function foo1({name,age, school='hust'}){
  console.log(name, age,school)
  return {animal:'pig',color:'red'}
}
let {animal,color} = foo1(user)
console.log(animal,color)